//
//  ProduceLineDetailViewController.m
//  MagictoLane
//
//  Created by 锋 on 2017/12/18.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "ProduceLineDetailViewController.h"
#import "ProduceDetailTableViewCell.h"
#import "ProduceDetailPointTableViewCell.h"
#import "ProduceDetailModel.h"
#import "ProduceSpotsModel.h"
#import "ProduceLineKindModel.h"
#import "ProduceMachinesModel.h"
#import "ProduceMachineNameModel.h"
#import "AddProductionLineViewController.h"
#import "FeedBackViewController.h"
#import "ShareManager.h"

@interface ProduceLineDetailViewController ()<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>
{
    UILabel *secondLineLabel;
    UILabel *thirdLineLabel;
    UILabel *nameLabel;
    UILabel *jobNameLabel;
    UIView *setUpView;
}
@property (nonatomic, strong) UITableView *productionLineDetailTableView;
@end

@implementation ProduceLineDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self setupProductionLineDetailTableView];
    [self requestProduceDetailData:_ID];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestProduceDetailData:_ID];
}

- (void)setupUI {
    if (_produceLineType == MYPRODUCELINETYPE) {
        UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bianji"] style:UIBarButtonItemStylePlain target:self action:@selector(onClickEditProduceLine)];
        rightBtn.tintColor = [UIColor whiteColor];
        self.navigationItem.rightBarButtonItem = rightBtn;
    } else {
        UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"dd"] style:UIBarButtonItemStylePlain target:self action:@selector(onClickMoreBtn)];
        rightBtn.tintColor = [UIColor whiteColor];
        self.navigationItem.rightBarButtonItem = rightBtn;
        self.navigationController.navigationBar.hidden = NO;
    }
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"产线详情";
    self.view.backgroundColor = RGB(242, 242, 242);
}

- (void)onClickMoreBtn {
    setUpView.hidden = !setUpView.hidden;
}

- (void)setupProductionLineDetailTableView {
    _productionLineDetailTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - STA_NAV_HEIGHT - SCREER_HeiX) style:UITableViewStyleGrouped];
    _productionLineDetailTableView.delegate = self;
    _productionLineDetailTableView.dataSource = self;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.000001)];
    view.backgroundColor = [UIColor whiteColor];
    _productionLineDetailTableView.tableHeaderView = view;
    _productionLineDetailTableView.tableFooterView = view;
    _productionLineDetailTableView.backgroundColor = [UIColor whiteColor];
    _productionLineDetailTableView.backgroundColor = RGB(242, 242, 242);
    _productionLineDetailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (@available(iOS 11.0, *)) {
        _productionLineDetailTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    _productionLineDetailTableView.estimatedRowHeight = 0;
    _productionLineDetailTableView.estimatedSectionHeaderHeight = 0;
    _productionLineDetailTableView.estimatedSectionFooterHeight = 0;
    [self.view addSubview:_productionLineDetailTableView];
    
    setUpView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-115*displaywidth, 0, 100*displaywidth, 100*displaywidth)];
    setUpView.backgroundColor = [UIColor whiteColor];
    setUpView.layer.cornerRadius = 5;
    setUpView.layer.masksToBounds = YES;
    setUpView.hidden = YES;
    [self.view addSubview:setUpView];
    NSArray *imgUrlAry = @[@"fenx",@"jubao"];
    NSArray *nameAry = @[@" 分享",@" 举报"];
    for (int i = 0; i < imgUrlAry.count; i++)
    {
        UIButton *btn = [ZCControl createButtonWithFrame:CGRectMake(0, 50*displaywidth*i, 100*displaywidth, 49*displaywidth) ImageName:imgUrlAry[i] Target:self Action:@selector(closeSetUpAction:) Title:nameAry[i] Font:13.0];
        [btn setTitleColor:RGB(77, 77, 77) forState:UIControlStateNormal];
        btn.tag = 80+i;
        [setUpView addSubview:btn];
        if (i != 1)
        {
            UIView *lineAAView = [[UIView alloc] initWithFrame:CGRectMake(0, (i+1)*50*displaywidth-1*displaywidth, 100*displaywidth, 1*displaywidth)];
            lineAAView.backgroundColor = LinesColor;
            [setUpView addSubview:lineAAView];
            
        }
    }
}

- (void)closeSetUpAction:(UIButton *)btn
{
    if (btn.tag == 80)
    {
        //分享
        ProduceDetailModel *detailModel = _produceDetailArr[0];
        ShareManager *shareManager = [ShareManager shareToManager];
        shareManager.title = @"分享产线";
        NSString *url = [LTools cacheForKey:@"loginHeadImg"];
        shareManager.imgUrl = url;
        shareManager.descriptionStr = detailModel.name;
        shareManager.webUrl = @"http://m.moform.cn/#/";
    }
    else
    {
        //举报
        FeedBackViewController *vc = [[FeedBackViewController alloc] init];
        vc.titleStr = @"举报";
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)requestProduceDetailData:(NSString *)ID {
    WEAKSELF;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HYBaseHandler requestProduceLineDetailParmers:@{@"id" : ID} Success:^(id obj)
     {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         if ([obj server_Getcodecheck])
         {
             NSLog(@"%@", obj);
             _produceDetailArr = [ProduceDetailModel mj_objectArrayWithKeyValuesArray:@[obj[@"data"]]];
             [_productionLineDetailTableView reloadData];
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         
     }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else if (section == 1) {
        ProduceDetailModel *model = _produceDetailArr[0];
        return model.machines.count + 1;
    } else if (section == 2) {
        ProduceDetailModel *model = _produceDetailArr[0];
        return model.spots.count + 2;
    } else {
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 65;
    } else {
        return 44;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
        return 5;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.000001;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ProduceDetailModel *detailModel = _produceDetailArr[0];
    if (indexPath.section == 0) {
        ProduceDetailTableViewCell *cell = [[NSBundle mainBundle] loadNibNamed:@"ProduceDetailTableViewCell" owner:self options:nil].lastObject;
        cell.detailNameLabel.text = detailModel.name;
        cell.detailKindLabel.text = detailModel.category.name;
        cell.detailPointLabel.text = [NSString stringWithFormat:@"%@元/天", detailModel.rent];
        
        return cell;
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            NSString *ID = @"cellID";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
            }
            if (!nameLabel) {
                nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH - 100, 44)];
                nameLabel.text = @"生产设备";
                nameLabel.textColor = UIColorHexValue(0x4D4D4D);
                nameLabel.backgroundColor = [UIColor whiteColor];
                nameLabel.font = [UIFont systemFontOfSize:16];
                nameLabel.textAlignment = NSTextAlignmentLeft;
                [cell.contentView addSubview:nameLabel];
            }
            if (!secondLineLabel) {
                secondLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 43, SCREEN_WIDTH, 1)];
                secondLineLabel.backgroundColor = RGB(238, 239, 238);
                [cell.contentView addSubview:secondLineLabel];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell;
        } else {
            NSString *ID = @"cellID1";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
            }
            if (![cell.contentView viewWithTag:indexPath.row + 200]) {
                UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 43, SCREEN_WIDTH - 15, 1)];
                lineLabel.tag = 200 + indexPath.row;
                lineLabel.backgroundColor = RGB(238, 239, 238);
                [cell.contentView addSubview:lineLabel];
            }
            if (indexPath.row == 4) {
                UILabel *lineLabel = [cell.contentView viewWithTag:indexPath.row + 200];
                lineLabel.hidden = YES;
            }
            ProduceMachinesModel *machinesModel = detailModel.machines[indexPath.row - 1];
            cell.textLabel.text = machinesModel.machine.name;
            cell.textLabel.textColor = UIColorHexValue(0x4D4D4D);
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@台", machinesModel.count];
            cell.detailTextLabel.textColor = UIColorHexValue(0x4D4D4D);
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell;
        }
        
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            NSString *ID = @"cellID2";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
            }
            if (!jobNameLabel) {
                jobNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH - 100, 44)];
                jobNameLabel.text = @"生产节点";
                jobNameLabel.textColor = UIColorHexValue(0x4D4D4D);
                jobNameLabel.backgroundColor = [UIColor whiteColor];
                jobNameLabel.font = [UIFont systemFontOfSize:16];
                jobNameLabel.textAlignment = NSTextAlignmentLeft;
                [cell.contentView addSubview:jobNameLabel];
            }
            if (!thirdLineLabel) {
                thirdLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 43, SCREEN_WIDTH, 1)];
                thirdLineLabel.backgroundColor = RGB(238, 239, 238);
                [cell.contentView addSubview:thirdLineLabel];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell;
        } else {
            ProduceDetailPointTableViewCell *cell = [[NSBundle mainBundle] loadNibNamed:@"ProduceDetailPointTableViewCell" owner:self options:nil].lastObject;
            if (indexPath.row == 1) {
                [cell.signBtn setImage:[UIImage imageNamed:@"guanlizhe"] forState:UIControlStateNormal];
                cell.nameLabel.text = @"管理者";
            } else {
                ProduceSpotsModel *spotsModel = detailModel.spots[indexPath.row - 2];
                cell.nameLabel.text = spotsModel.name;
                cell.detailNameLabel.text = spotsModel.remark;
                [cell.signBtn setTitle:[NSString stringWithFormat:@"%ld", indexPath.row - 1] forState:UIControlStateNormal];
            }
            if (indexPath.row == 4) {
                cell.lineLabel.hidden = YES;
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell;
        }
    } else {
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)onClickEditProduceLine {
    AddProductionLineViewController *addProduceLineVC = [[AddProductionLineViewController alloc] init];
    ProduceDetailModel *detailModel = _produceDetailArr[0];
    addProduceLineVC.produceType = PRODUCEDETAILTYPE;
    addProduceLineVC.produceDetailTitle = detailModel.name;
    addProduceLineVC.detailModel = detailModel;
    [self.navigationController pushViewController:addProduceLineVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
